package br.com.empeng.model.persistencia.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;

import br.com.empeng.model.entidades.Fornecedor;
import br.com.empeng.model.persistencia.util.HibernateUtil;

public class FornecedorDao {

	private Session sessao = HibernateUtil.abrirSessao();
	
	public void salvar(Fornecedor fornecedor){
		//Insert into Fornecedor (id, nome, login, senha) values (10,'aaa','bbb','ccc')
		//Update fornecedor set nome='aaa', login='bbb', senha='ccc' where id=10
		sessao.merge(fornecedor);
		sessao.beginTransaction().commit(); //executa
		
	}
	
		
	
	public void excluir(Fornecedor fornecedor){
		//ex: Delete from Fornecedor where id=10
		sessao.delete(fornecedor);
		sessao.beginTransaction().commit();
	}
	
	public Fornecedor buscarPorId(Integer id){
		//Ex: Select *  from fornecedor where id=10
		return (Fornecedor)sessao.load(Fornecedor.class, id);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Fornecedor> buscarTodos(){
		
		//Selec * from fornecedor
		Criteria criteria = sessao.createCriteria(Fornecedor.class).addOrder(Order.asc("nome"));
		return criteria.list();
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Fornecedor> buscarPorExemplo(Fornecedor fornecedor){
		//Select * from 
		Criteria criteria = sessao.createCriteria(Fornecedor.class).addOrder(Order.asc("nome"));
		
		//Where nome='maria' and login='mar' and senha ='123'
		Example example =  Example.create(fornecedor);
		
		criteria.add(example);
		
		return criteria.list();	
		
	}
	 
	
	
}
